package com.example.onlinemonitoring.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.onlinemonitoring.Util.CommPortManager;
import com.example.onlinemonitoring.model.Applogin;
import com.example.onlinemonitoring.model.Appstation;
import com.example.onlinemonitoring.model.common.MyResult;
import com.example.onlinemonitoring.serviceImpl.ApploginServiceImpl;
import com.example.onlinemonitoring.serviceImpl.AppstationServiceImpl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.example.onlinemonitoring.Util.LanguageManager;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import tk.mybatis.mapper.entity.Example;

@Api(value = "登录接口", tags = { "登录接口" })
@Controller
public class loginController {
    @Autowired
    ApploginServiceImpl ApploginServiceImpl;
    @Autowired
    AppstationServiceImpl AppstationServiceImpl;
    @Autowired
    LanguageManager languageManager;
   // @Autowired
   // CommPortManager Commlanguage;

    @ResponseBody
    @GetMapping("/selectAccount")
    public Applogin selectApplogin(
            @RequestParam(value = "name", required = true) String name,
            @RequestParam(value = "password", required = true) String password) {
        {
            Example example = new Example(Appstation.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("contract", password);
            List<Appstation> list = AppstationServiceImpl.selectByExample(example);

            if (list.size() != 0) {
                Appstation as = new Appstation();
                as.setId(1);
                as.setChannelnum(CommPortManager.isnull(list.get(0).getChannelnum()) + 1);
                AppstationServiceImpl.Update(as, true);

            }
            Appstation as = new Appstation();
            as.setId(1);
            as = AppstationServiceImpl.selectByPrimaryKey(as);
            languageManager.myInit(as.getLanguage());
            CommPortManager.Language = as.getLanguage();
            
        }

        Example example = new Example(Applogin.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("loginname", name);
        criteria.andEqualTo("loginpassword", password);
        return ApploginServiceImpl.selectOneByExample(example);
    }

    @ApiOperation(value = "查询数据", notes = "查询数据")
    @ResponseBody
    @GetMapping("/selectApplogin")
    public MyResult<Applogin> selectApplogin() {
        return ApploginServiceImpl.getList();
    }

    @ApiOperation(value = "修改user密码", notes = "修改user密码")
    @ResponseBody
    @RequestMapping(value = "/updateApplogin", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public MyResult<Integer> updateApplogin(@RequestBody String jsonParam) {
        JSONObject object = JSONObject.parseObject(jsonParam);
        Applogin record = object.toJavaObject(Applogin.class);
        record.setLoginname("user");
        MyResult<Integer> m = new MyResult<Integer>();
        if (ApploginServiceImpl.Update(record, false) >= 1) {
            m.setMsg(languageManager.getLan().getModifySuccess());
            m.setCode(0);
        } else {
            m.setMsg(languageManager.getLan().getModifyFailed());
            m.setCode(1);
        }
        return m;
    }

    @ApiOperation(value = "新增数据", notes = "新增数据")
    @ResponseBody
    @RequestMapping(value = "/addApplogin", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public MyResult<Integer> addApplogin(@RequestBody String jsonParam) {
        JSONObject object = JSONObject.parseObject(jsonParam);
        Applogin record = object.toJavaObject(Applogin.class);
        MyResult<Integer> m = new MyResult<Integer>();
        if (ApploginServiceImpl.insertSelective(record) >= 1) {
            m.setMsg(languageManager.getLan().getAddSuccess());
            m.setCode(0);
        } else {
            m.setMsg(languageManager.getLan().getAddFailed());
            m.setCode(1);
        }
        return m;
    }

    @ApiOperation(value = "删除数据", notes = "删除数据")
    @ResponseBody
    @GetMapping("/deleteApplogin")
    public MyResult<Integer> deleteApplogin(@RequestParam(value = "id", required = false) Long id) {
        Applogin record = new Applogin();
        record.setId(id);
        MyResult<Integer> m = new MyResult<Integer>();
        if (ApploginServiceImpl.deleteByPrimaryKey(record) >= 1) {
            m.setMsg(languageManager.getLan().getDeleteSuccess());
            m.setCode(0);
        } else {
            m.setMsg(languageManager.getLan().getDeleteFailed());
            m.setCode(1);
        }
        return m;
    }
}
